package com.sgai.zero_robot.ui.login;

import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;

import androidx.annotation.NonNull;

import com.sgai.zero_robot.R;
import com.sgai.zero_robot.base.BaseActivity;
import com.sgai.zero_robot.base.BaseViewModel;
import com.sgai.zero_robot.core.RobotManagerFactory;
import com.sgai.zero_robot.databinding.ActivityLoginBinding;
import com.sgai.zero_robot.ui.main.MainActivity;
import com.sgai.zero_robot.utils.LogUtil;
import com.sgai.zero_robot.utils.SPUtils;

public class LoginActivity extends BaseActivity<ActivityLoginBinding, BaseViewModel> implements View.OnClickListener {


    private ObjectAnimator rotateAnimator;

    @Override
    public void initViews(Bundle savedInstanceState) {
        RobotManagerFactory.getInstance().init();
        String password = SPUtils.getInstance().get("password", "");
        String username = SPUtils.getInstance().get("username", "");
        SPUtils.getInstance().put("checkpassword", true);
        Boolean checkpassword = SPUtils.getInstance().get("checkpassword", false);
        if (checkpassword!=null){
            mBinding.cbPassword.setChecked(checkpassword);
        }
//        mBinding.etPassword.setText(password);
//        mBinding.etUsername.setText(username);
        startTimer();
        mBinding.btLogin.setOnClickListener(this);
    }

    @Override
    public void loadData() {

    }

    private final int firstDelay = 100;
    private final int delay = 1000;
    private int temp = 0;
    private boolean isRunning = false;

    private void setSuccess(ImageView imageView) {
        imageView.setImageDrawable(getResources().getDrawable(R.mipmap.init_success));
        stopRotateAnimation(imageView);
        temp = 0;
    }

    private void setFailed(ImageView imageView) {
        imageView.setImageDrawable(getResources().getDrawable(R.mipmap.init_failed));
        stopRotateAnimation(imageView);
        temp = 0;
    }

    private void setLoading(ImageView imageView) {
        imageView.setImageDrawable(getResources().getDrawable(R.mipmap.init_loading));
        startRotateAnimation(imageView);
    }

    private void startRotateAnimation(View view) {
        if (rotateAnimator != null && rotateAnimator.isRunning()) {
            rotateAnimator.cancel();
            rotateAnimator = null;
        }
        rotateAnimator = ObjectAnimator.ofFloat(view, "rotation", 0f, 360f);
        rotateAnimator.setDuration(1000); // 一个周期 2 秒
        rotateAnimator.setRepeatCount(ValueAnimator.INFINITE); // 无限循环
        rotateAnimator.setInterpolator(new LinearInterpolator()); // 匀速
        rotateAnimator.start();
    }

    private void stopRotateAnimation(View view) {
        if (rotateAnimator != null && rotateAnimator.isRunning()) {
            rotateAnimator.cancel();
            rotateAnimator = null;
        }
        view.clearAnimation();
    }

    private Handler handler = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            temp++;
            LogUtil.e("#####", "what = " + msg.what + "   temp = " + temp);
            if (msg.what == 1) {
                if (temp == 1) {
                    setLoading(mBinding.ivConnect);
                    handler.sendEmptyMessageDelayed(1, delay);
                } else {
                    if (RobotManagerFactory.getInstance().isConnect()) {
                        setSuccess(mBinding.ivConnect);
                        handler.sendEmptyMessageDelayed(2, firstDelay);
                    } else {
                        if (temp >= 30) {
                            setFailed(mBinding.ivConnect);
                            setFailed(mBinding.ivMove);
                            setFailed(mBinding.ivSwing);
                            setFailed(mBinding.ivLaser);
                            setFailed(mBinding.ivInertial);
                            setFailed(mBinding.ivLight);
                            isRunning = false;
                        } else {
                            handler.sendEmptyMessageDelayed(1, delay);
                        }
                    }
                }

            } else if (msg.what == 2) {
                if (temp == 1) {
                    setLoading(mBinding.ivMove);
                    handler.sendEmptyMessageDelayed(2, delay);
                } else {
                    if (RobotManagerFactory.getInstance().alarmInfoMap.containsKey("2")) {
                        setFailed(mBinding.ivMove);
                        handler.sendEmptyMessageDelayed(3, firstDelay);
                    } else if (RobotManagerFactory.getInstance().alarmInfoMap.containsKey("3")) {
                        setFailed(mBinding.ivMove);
                        handler.sendEmptyMessageDelayed(3, firstDelay);
                    } else if (RobotManagerFactory.getInstance().alarmInfoMap.containsKey("8")) {
                        setFailed(mBinding.ivMove);
                        handler.sendEmptyMessageDelayed(3, firstDelay);
                    } else if (RobotManagerFactory.getInstance().alarmInfoMap.containsKey("9")) {
                        setFailed(mBinding.ivMove);
                        handler.sendEmptyMessageDelayed(3, firstDelay);
                    } else {
                        setSuccess(mBinding.ivMove);
                        handler.sendEmptyMessageDelayed(3, firstDelay);
                    }
                }

            } else if (msg.what == 3) {
                if (temp == 1) {
                    setLoading(mBinding.ivSwing);
                    handler.sendEmptyMessageDelayed(3, delay);
                } else {
                    if (RobotManagerFactory.getInstance().alarmInfoMap.containsKey("3")) {
                        setFailed(mBinding.ivSwing);
                        handler.sendEmptyMessageDelayed(4, firstDelay);
                    } else if (RobotManagerFactory.getInstance().alarmInfoMap.containsKey("4")) {
                        setFailed(mBinding.ivSwing);
                        handler.sendEmptyMessageDelayed(4, firstDelay);
                    } else if (RobotManagerFactory.getInstance().alarmInfoMap.containsKey("5")) {
                        setFailed(mBinding.ivSwing);
                        handler.sendEmptyMessageDelayed(4, firstDelay);
                    } else if (RobotManagerFactory.getInstance().alarmInfoMap.containsKey("6")) {
                        setFailed(mBinding.ivSwing);
                        handler.sendEmptyMessageDelayed(4, firstDelay);
                    } else {
                        setSuccess(mBinding.ivSwing);
                        handler.sendEmptyMessageDelayed(4, firstDelay);
                    }
                }
            } else if (msg.what == 4) {
                if (temp == 1) {
                    setLoading(mBinding.ivLaser);
                    handler.sendEmptyMessageDelayed(4, delay);
                } else {
                    if (RobotManagerFactory.getInstance().alarmInfoMap.containsKey("10")) {
                        setFailed(mBinding.ivLaser);
                        handler.sendEmptyMessageDelayed(5, firstDelay);
                    } else if (RobotManagerFactory.getInstance().alarmInfoMap.containsKey("11")) {
                        setFailed(mBinding.ivLaser);
                        handler.sendEmptyMessageDelayed(5, firstDelay);
                    } else if (RobotManagerFactory.getInstance().alarmInfoMap.containsKey("12")) {
                        setFailed(mBinding.ivLaser);
                        handler.sendEmptyMessageDelayed(5, firstDelay);
                    } else if (RobotManagerFactory.getInstance().alarmInfoMap.containsKey("13")) {
                        setFailed(mBinding.ivLaser);
                        handler.sendEmptyMessageDelayed(5, firstDelay);
                    } else {
                        setSuccess(mBinding.ivLaser);
                        handler.sendEmptyMessageDelayed(5, firstDelay);
                    }
                }
            } else if (msg.what == 5) {
                if (temp == 1) {
                    setLoading(mBinding.ivInertial);
                    handler.sendEmptyMessageDelayed(5, delay);
                } else {
                    if (RobotManagerFactory.getInstance().alarmInfoMap.containsKey("14")) {
                        setFailed(mBinding.ivInertial);
                        handler.sendEmptyMessageDelayed(6, firstDelay);
                    } else if (RobotManagerFactory.getInstance().alarmInfoMap.containsKey("15")) {
                        setFailed(mBinding.ivInertial);
                        handler.sendEmptyMessageDelayed(6, firstDelay);
                    } else if (RobotManagerFactory.getInstance().alarmInfoMap.containsKey("16")) {
                        setFailed(mBinding.ivInertial);
                        handler.sendEmptyMessageDelayed(6, firstDelay);
                    } else {
                        setSuccess(mBinding.ivInertial);
                        handler.sendEmptyMessageDelayed(6, firstDelay);
                    }
                }
            } else if (msg.what == 6) {
                if (temp == 1) {
                    setLoading(mBinding.ivLight);
                    handler.sendEmptyMessageDelayed(6, delay);
                } else {
                    if (RobotManagerFactory.getInstance().alarmInfoMap.containsKey("17")) {
                        setFailed(mBinding.ivLight);
                    } else if (RobotManagerFactory.getInstance().alarmInfoMap.containsKey("18")) {
                        setFailed(mBinding.ivLight);
                    } else {
                        setSuccess(mBinding.ivLight);
                    }
                    isRunning = false;
                }
            }
        }
    };

    private void startTimer() {
        isRunning = true;
        handler.sendEmptyMessageDelayed(1, firstDelay);
    }


    private void stopTimer() {
        isRunning = false;
        handler.removeMessages(1);
        handler.removeMessages(2);
        handler.removeMessages(3);
        handler.removeMessages(4);
        handler.removeMessages(5);
        handler.removeMessages(6);
        handler.removeMessages(7);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        stopTimer();
    }

    @Override
    public void onClick(View v) {
        if (v == mBinding.btLogin) {
            String userName = mBinding.etUsername.getText().toString();
            if (TextUtils.isEmpty(userName)) {
                showAlarm(2, "警告", "账号不能为空。",null);
                return;
            }
            String password = mBinding.etPassword.getText().toString();
            if (TextUtils.isEmpty(password)) {
                showAlarm(2, "警告", "密码不能为空。",null);
                return;
            }
//            if (isRunning){
//                showAlarm(2, "警告", "自检未完成，是否继续？", new AlarmDialog.AlarmCallback() {
//                    @Override
//                    public void onKnow() {
//                       login(userName,password);
//                    }
//                });
//            }else{
//                login(userName,password);
//            }
            login(userName,password);
        }
    }
    private void login(String userName,String password){
        if ("gwznjljqr".equals(userName) && "JQRjqr@2025".equals(password)) {
            if (mBinding.cbPassword.isChecked()) {
                SPUtils.getInstance().put("checkpassword", true);
                SPUtils.getInstance().put("password", password);
            } else {
                SPUtils.getInstance().put("checkpassword", false);
                SPUtils.getInstance().remove("password");
            }
            SPUtils.getInstance().put("username", userName);
            startActivity(new Intent(LoginActivity.this, MainActivity.class));
            finish();
        } else {
            showAlarm(1, "警告", "账号或者密码错误。",null);
        }
    }
}